/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormServicios.java
 *
 * Created on 24/11/2010, 02:00:27 AM
 */

package pe.edu.upla.lamerced.desk.view.maestro;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.Utiles;
import pe.edu.upla.lamerced.bean.Servicios;
import pe.edu.upla.lamerced.bean.Tipotarificacion;
import pe.edu.upla.lamerced.service.ServiciosService;

/**
 *
 * @author elvis
 */
public class FormServicios extends javax.swing.JInternalFrame {

    private JDesktopPane escritorio;
    private Servicios servicios;
    private List<Tipotarificacion> listaTarificacion;
    private ServiciosService serviciosService;

    /** Creates new form FormServicios */
    public FormServicios() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      btnGrabar = new javax.swing.JButton();
      btnCancelar = new javax.swing.JButton();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      jLabel3 = new javax.swing.JLabel();
      jLabel4 = new javax.swing.JLabel();
      txtNombre = new javax.swing.JTextField();
      txtDescripcion = new javax.swing.JTextField();
      txtTarifa = new javax.swing.JTextField();
      cbTarificacion = new javax.swing.JComboBox();
      jLabel5 = new javax.swing.JLabel();

      setClosable(true);
      setTitle("Servicios");

      btnGrabar.setText("Grabar");
      btnGrabar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnGrabarActionPerformed(evt);
         }
      });

      btnCancelar.setText("Cancelar");
      btnCancelar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCancelarActionPerformed(evt);
         }
      });

      jLabel1.setText("Nombre");

      jLabel2.setText("Descripcion");

      jLabel3.setText("Tarifa");

      jLabel4.setText("Tarificacion");

      txtNombre.setText("jTextField1");

      txtDescripcion.setText("jTextField2");

      txtTarifa.setText("jTextField3");

      cbTarificacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

      jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
      jLabel5.setText("Servicios");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addGap(42, 42, 42)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jLabel1)
                     .addComponent(jLabel2)
                     .addComponent(jLabel3)
                     .addComponent(jLabel4))
                  .addGap(33, 33, 33))
               .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addContainerGap(58, Short.MAX_VALUE)
                  .addComponent(btnGrabar)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jLabel5)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                  .addComponent(cbTarificacion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addComponent(txtTarifa)
                  .addComponent(txtDescripcion)
                  .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
               .addGroup(layout.createSequentialGroup()
                  .addGap(36, 36, 36)
                  .addComponent(btnCancelar)))
            .addContainerGap(39, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel5)
            .addGap(21, 21, 21)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(jLabel1)
                  .addGap(18, 18, 18)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(jLabel2)
                     .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(18, 18, 18)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(jLabel3)
                     .addComponent(txtTarifa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(18, 18, 18)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(jLabel4)
                     .addComponent(cbTarificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
               .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(btnGrabar)
               .addComponent(btnCancelar))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
       cargaListaform();
       this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
      servicios.setDetalle(Utiles.nullToBlank(txtDescripcion.getText()));
      servicios.setNombre(Utiles.nullToBlank(txtNombre.getText()));
      servicios.setTarifa(new Long(txtTarifa.getText()));
      servicios.setEstado(1);
      servicios.setTipotarificacion(listaTarificacion.get(cbTarificacion.getSelectedIndex()));
      serviciosService.registrarServicio(servicios);
      JOptionPane.showMessageDialog(this, "Se ha registrado el serviio");
      cargaListaform();
       this.dispose();
    }//GEN-LAST:event_btnGrabarActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnCancelar;
   private javax.swing.JButton btnGrabar;
   private javax.swing.JComboBox cbTarificacion;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JLabel jLabel4;
   private javax.swing.JLabel jLabel5;
   private javax.swing.JTextField txtDescripcion;
   private javax.swing.JTextField txtNombre;
   private javax.swing.JTextField txtTarifa;
   // End of variables declaration//GEN-END:variables

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }

   public void cargaInicial(){
      serviciosService = (ServiciosService)Inicial.getFactory().getBean("serviciosService");
      listaTarificacion = serviciosService.listaTipoTarificacion();
      txtDescripcion.setText(Utiles.nullToBlank(servicios.getDetalle()));
      txtNombre.setText(Utiles.nullToBlank(servicios.getNombre()));
      txtTarifa.setText(Utiles.nullToBlank(servicios.getTarifa()));
      cbTarificacion.removeAllItems();
      for(Tipotarificacion tarificacion : listaTarificacion){
         cbTarificacion.addItem(tarificacion.getNombre());
      }
      if(servicios.getTipotarificacion() != null)
      cbTarificacion.setSelectedItem(Utiles.nullToBlank(servicios.getTipotarificacion().getNombre()));
   }

   public Servicios getServicios() {
      return servicios;
   }

   public void setServicios(Servicios servicios) {
      this.servicios = servicios;
   }
   public List<Tipotarificacion> getListaTarificacion() {
      return listaTarificacion;
   }
   public void setListaTarificacion(List<Tipotarificacion> listaTarificacion) {
      this.listaTarificacion = listaTarificacion;
   }
   
   public void cargaListaform(){
      ListaServicios form = new ListaServicios();
      form.setEscritorio(escritorio);
      form.cargainicial();
      escritorio.add(form);
      form.setVisible(true);
   }
}
